<?php
namespace Plu\Library\Models\Platform\Media;

use Phalcon\Mvc\Model,
    Phalcon\Db\Column;

class NewMedia extends Model {
    public $Id;
    public $RoomId;
    public $TotalViews;
    public $GameId;
    public $Title;
    public $Cover;
    public $Url;
    public $TimeSpan;
    public $CreateTime;
    public $CreateUser;
    public $DeleteUser;
    public $DeleteTime;
    public $ExamineStatus;
    public $LiveSourceType;
    public $PlayId;
    public $LiveStreamType;
    public $RoomName;

    const ExamineStatus = array(
        //all
        'all'           => -1,
        //初始
        'None'          => 0,
        //转码
        'TransCoding'   => 1,
        //审核中
        'Examine'       => 2,
        //未通过
        'Refuse'        => 3,
        //通过
        'Pass'          => 4,
        //已删除
        'Deleted'       => 5,
        //待复核
        'Review'        => 6
    );

    const Filter = [
        //所有
        'all'    => 'in (4,5)', 
        //通过
        'pass'   => '= 4', 
        //删除
        'delete' => '=5',    
        //审核中
        'Examine'  => '=2',
        //未通过
        'refuse' => '=3'
    ];

    public function initialize() {
        $this->setReadConnectionService('db.platform.slave');
        $this->setWriteConnectionService('db.platform');
        $this->allowEmptyStringValues(['Url', 'Cover']); 
    }

    public function getSource() {
        return 'ReplayVideo';
    }

    public function timeFormat($TimeSpan) {

    }

    static public function getVideos($offset, $limit, $filter) {
        $where = array(
            '(GameId = :suipai: or GameId = :yuzhai: or GameId = :huwai: or GameId = :mengchong:) and ExamineStatus in (4,5)',
            'bind'  =>  array(
                'suipai'    =>  119,
                'yuzhai'    =>  127,
                'huwai'     =>  128,
                'mengchong' =>  129,
            )
        );
        $count = self::count($where);
        $where['order'] = 'Id desc';
        $where['limit'] = $limit;
        $where['offset'] = $offset;
        $items = self::find($where); 

        if($items) {
            $items = $items->toArray();

        } else {
            $items = array();
        }

        $res = array($count,$items);

        return $res;
    }

    static public function getVideosByRoomIds($roomId, $offset, $limit, $filter) {
        if(is_array($roomId)) {
            $roomId = $roomId[0];
        }

        $where = array(
            '(RoomId = :roomId:) and ExamineStatus in (2,3,4)',
            'bind'  =>  array(
                'roomId'    =>  $roomId,
            ),
            'order'     =>  'Id desc',
            'limit'     =>  $limit,
            'offset'    =>  $offset,
        );
        $items = self::find($where);
        $count = self::count($where);

        if($items) {
            $items = $items->toArray();
        } else {
            $items = array();
        }

        $res = array($count,$items);

        return $res;
    }

    // DELETE
    static public function deleteVideo($id, $uid) {
        date_default_timezone_set('Asia/Shanghai');
        $where = array(
            'Id = :id:',
            'bind'  =>  array(
                'id'    =>  $id,
            ),
        );
        $item = self::findFirst($where);
        $item->ExamineStatus = 3;
        $item->DeleteTime = date("Y-m-d H:i:s");
        $item->DeleteUser = $uid;

        $res = $item->save();
    }


    static public function OptionVideo($id, $uid, $option) {
        date_default_timezone_set('Asia/Shanghai');

        $where = array(
            'Id = :id:',
            'bind'  =>  array(
                'id'    =>  $id,
            ),
        );
        $item = self::findFirst($where);
        $item->ExamineStatus = self::ExamineStatus[$option];
        $item->DeleteTime = date("Y-m-d H:i:s");
        $item->DeleteUser = $uid;

        $result = $item->save();
        return $result;

    }

    static public function UpdateTitle($id, $title) {
        date_default_timezone_set('Asia/Shanghai');
        $where = array(
            'Id = :id:',
            'bind'  =>  array(
                'id'    =>  $id,
            ),
        );
        $item = self::findFirst($where);
        $item->Title = $title;
        $res = $item->save();
        return $res;
    }

    static public function UpdateTitleByLiveId($LiveId, $title) {
        //date_default_timezone_set('Asia/Shanghai');
        $where = array(
            'PlayId = :id:',
            'bind'  =>  array(
                'id'    =>  $LiveId,
            ),
        );
        $item = self::findFirst($where);
        $item->Title = $title;
        $res = $item->save();
        if ($res) {
            return $res;
        } else {
            echo 'db_error';
            $msg = $item->getMessages();
            print_r($msg);

        }

    }


    public function getVideoByFilter($offset, $limit, $uploadtime_start = null, $uploadtime_end = null, $title, $roomId) {

        $condition = [];
        $bind = [];

        // uploadtime search
        if($uploadtime_start) {
            $startTime = date('Y-m-d', strtotime($uploadtime_start));
            $condition[] = " CreateTime > :start: ";
            $bind['start'] = $startTime;
        }

        if ($uploadtime_end) {
            $endTime   = date('Y-m-d', (strtotime($uploadtime_end) + 86400));
            $condition[] = " CreateTime < :end: ";
            $bind['end'] = $endTime;
        }

        // roomId search
        if ($roomId) {
            $condition[] = " RoomId = :roomId: ";
            $bind['roomId'] = $roomId;
        }

        // title search
        if ($title) {
            $condition[] = " Title = :title: ";
            $bind['title'] = $title;
        } 

        $condition = join(" AND ", $condition);

        $where = [
            'conditions' => $condition,
            'bind'  => $bind
        ];
        $count = self::count($where);

        $where['order'] = 'Id DESC';
        $where['limit'] = $limit;
        $where['offset'] = $offset;
        
        $items = self::find($where);

        if ($items) {
            $items = $items->toArray();
        } else {
            $items = [];
        }

        $res = [$count, $items];

        return $res;

    }

    static public function getTime($LiveId) {
        $where = [
            'PlayId = :PlayId: ORDER BY CreateTime DESC',
            'bind'  =>  [
                'PlayId'    =>  $LiveId,
            ],
        ];
        $result = self::findFirst($where);

        return $result;
    }

    static public function getItem($Id) {
        $where = [
            'Id = :Id: ORDER BY CreateTime DESC',
            'bind'  =>  [
                'Id'    =>  $Id,
            ],
        ];
        $result = self::findFirst($where);

        return $result;

    }
}
?>
